﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Shoozla.ViewModel;

namespace Shoozla.View
{
    /// <summary>
    /// Interaction logic for ProgressBarView.xaml
    /// </summary>
    public partial class ProgressBarView : Window
    {
        public double Maximum {
            get { if (mainPB != null) return mainPB.Maximum; return 0; }
            set { if (mainPB != null) mainPB.Maximum = value; } 
        }
        
        public double Value
        {
            get { if (mainPB != null) return mainPB.Value; return 0; }
            set { if (mainPB != null) mainPB.Value = value; } 
        }


        public ProgressBarView()
        {
            InitializeComponent();
            DataContext = new ProgressBarViewModel();
        }

        public void ShowProgressBar(int max, Window owner)
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                Maximum = max;
                //mainPB.Maximum = Maximum;
                mainPB.Minimum = 0;
                Owner = owner;
                this.Show();
            }));         
        }

        public void PerfomStepsToEnd()
        {
            //simulate the end
            this.Dispatcher.Invoke((Action)(() =>
            {
                while (mainPB.Value < mainPB.Maximum)
                    PerformStep();
            }));
        }

        public void PerformStep()
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                if (++mainPB.Value >= mainPB.Maximum)
                    this.Close();
                else
                    currentTB.Text = ""+mainPB.Value;
            }));
        }
    }
}
